package com.example.mscanner;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.example.mlscan.ScanConfig;
import com.example.mlscan.ScanManager;
import com.example.mlscan.callback.ScanCallback;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnScanDefault;
    private Button btnScanCustom;
    private TextView tvResults;

//    private ImageView imageView;
//    private EditText editText;
//    private CheckBox checkbox;
//    private CheckBox checkbox2;
//    private Spinner mSpColorBlack;
//    private Spinner mSpColorWhite;
//    private Spinner mSpMargin;

    private String error_correction_level;
    private int margin = 0;
    private int color_black = Color.BLACK;
    private int color_white = Color.WHITE;
    private Spinner mSpErrorCorrectionLevel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        requestCameraPerm();
    }

    private void initView() {
        btnScanDefault = (Button) findViewById(R.id.btn_scan_default);
        btnScanCustom = (Button) findViewById(R.id.btn_scan_custom);
        btnScanDefault.setOnClickListener(this);
        btnScanCustom.setOnClickListener(this);
        tvResults = (TextView) findViewById(R.id.tv_results);

    }

    public void requestCameraPerm() {
        //判断权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.CAMERA}, 10010);
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_scan_default) {

            ScanConfig scanConfig = new ScanConfig.Builder().isOnlyReturnValAutoSkip(false).builder();
            ScanManager.startScan(this,scanConfig,new ScanCallback() {
                @Override
                public void onActivityResult(int resultCode,Intent data) {
                    handlerResult(resultCode, data);
                }


            });
        }else if (view.getId() == R.id.btn_scan_custom) {
            //跳转到自定义界面
//            startActivity(new Intent(this, CustomConfigActivity.class));
        }
    }

    private void handlerResult(int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        switch (resultCode) {
            default:
                break;
            case ScanManager.RESULT_SUCCESS:
                ArrayList<String> results = data.getStringArrayListExtra(ScanManager.INTENT_KEY_RESULT_SUCCESS);
                StringBuilder resultStr = new StringBuilder();
                for (int i = 0; i < results.size(); i++) {
                    resultStr.append("第" + (i + 1) + "条：");
                    resultStr.append(results.get(i));
                    resultStr.append("\n");
                }
                tvResults.setText(resultStr.toString());
                break;
            case ScanManager.RESULT_FAIL:
                String resultError = data.getStringExtra(ScanManager.INTENT_KEY_RESULT_ERROR);
                showToast(resultError);
                break;
            case ScanManager.RESULT_CANCLE:
                showToast("取消扫码");
                break;
        }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }



}